package com.maxuxu.scihome.controller.document;

import com.maxuxu.scihome.pojo.Document;
import com.maxuxu.scihome.pojo.Message;
import com.maxuxu.scihome.pojo.Pages;
import com.maxuxu.scihome.service.DocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @className: DocumentQueryController
 * @author: Maxuxu
 * @date: 2022/7/1 22:43
 * @description: 查找文档的控制类
 */
@RestController
@Slf4j
public class DocumentQueryController {
    @Autowired
    DocumentService documentService;
    @Autowired
    Document document;
    @Autowired
    Message message;

    /**
     * 分页查询所有的数据
     * @return
     */
    @PostMapping("/document/select")
    public Pages findAll(long current, long size){
        return documentService.selectPage(current, size);
    }

    /**
     * 查询指定用户信息
     * @param title
     * @return
     */
    @GetMapping("/document/selectone")
    public Document findone(String title) {
        document.setTitle(title);
        Document document1 = documentService.selectByAny(document);
        return document1;
    }

    /**
     * 移除文档
     * @param request
     * @return
     */
    @GetMapping("/document/removeDocument")
    public Message removeDocu(HttpServletRequest request) {
        String ids = request.getParameter("ids").trim();
        String[] split = ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s:
                split) {
            Integer target = Integer.valueOf(s);
            list.add(target);
        }
        documentService.deletDocument(list);
        message.setJson("删除成功"+ids);
        return message;
}

    /**
     * 分页查询数据（考虑一对多关系，重写分页方法）
     * @param current
     * @param size
     * @return
     */
    @PostMapping("/document/findpage")
    public Pages selectPage2(long current, long size){
        return documentService.selectpage2(current, size);
    }
}
